<template>
        <el-card shadow="never" style="max-height: 70vh; overflow: auto">
            <el-form ref="form" :model="form" :rules="rules" label-width="90px" :disabled="disabled">
                <el-form-item label="id:" prop="id" v-show="false">
                    <el-input v-model="form.id" disabled />
                </el-form-item>
                <el-form-item label="模块分类:" prop="type" @change="onChangeType">
                    <el-radio-group v-model="form.type">
                        <el-radio v-for="(item, index) in type" :key="index" :label="item.value">{{ item.label }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="所属上级:" prop="pid" v-if="[1, 4].includes(form.type)">
                    <SelectPage v-model="form.pid" placeholder="请选择所属上级分类" :filter="{type: {value: form.type, op: '='} }"
                                :url="'/admin/px.category/selectPage'"
                                show_field="name" show_id="id" query_field="id" :multiple="false" />
                </el-form-item>
                <el-form-item label="分类名称:" prop="name">
                    <el-input v-model="form.name" placeholder="请输入分类名称" clearable />
                </el-form-item>
                <el-form-item label="分类图标:" prop="image">
                    <image-upload v-model="form.image"></image-upload>
                </el-form-item>
                <el-form-item label="分类描述:" prop="desc">
                    <el-input v-model="form.desc" placeholder="请输入分类描述" clearable />
                </el-form-item>
                <el-form-item label="排序:" prop="weigh">
                    <el-input-number v-model="form.weigh" :step="1" :min="0"></el-input-number>
                </el-form-item>
                <el-form-item label="状态:" prop="status">
                    <el-select v-model="form.status" placeholder="请选择状态" clearable>
                        <el-option v-for="(item, index) in status" :key="index" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm">确认 </el-button>
                    <el-button type="warning" @click="cancel">取消 </el-button>
                </el-form-item>
            </el-form>
        </el-card>
</template>

<script>
import { regionData } from 'element-china-area-data'
export default {
    name: 'EditForm',
    props: {
        findFormData: {
            type: Object,
            default: {}
        },
        editFormParams: {
            type: Object,
            default: {}
        },
        // 表单是否禁用
        disabled: {
            type: Boolean,
            default: false,
        },
    },
    data() {
        return {
            // 编辑表单验证规则
            rules: {
                type: [{ required: true, message: "请选择模块分类", trigger: "blur" }],
                name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
                status: [{ required: true, message: "请选择状态", trigger: "blur" }],
            },
            form: {
                type: 4
            },
            // 区域选择配置
            areaOptions: regionData,
            status:[
               { label: "正常", value: 1 },
               { label: "禁用", value: 2 },
            ],
            type:[
                { label: "培训分类", value: 4 },
            ],
        }
    },
    watch: {
        findFormData: {
           handler(nval) {
                if (JSON.stringify(nval) !== '{}') {
                    this.$nextTick(() => {
                        this.form = nval
                    })
                }
            },
            immediate: true,
            deep: false,
        },
    },
    methods: {
        // 取消按钮
        cancel() {
            this.$emit('editFormCancel')
        },
        submitForm() {
            this.$refs['form'].validate((valid, obj) => {
                if (valid) {
                    this.$emit('submitForm', this.form)
                }
            })
        },
        // 表单重置
        reset() {
            this.resetForm('form')
        },
        onChangeType(e) {
            this.form.type = undefined
            let timer = setTimeout(()=>{
                this.form.type = e
                clearTimeout(timer)
            }, 100)
        }
    }
}
</script>
